package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.Dao;
import com.mohe.dangjian.model.Remind;
import com.mohe.dangjian.service.interfaces.IRemind;
import com.mohe.dangjian.utils.Page;

public class RemindImpl implements IRemind {
	
	private Dao<Remind> remindDao;

	public Dao<Remind> getRemindDao() {
		return remindDao;
	}

	public void setRemindDao(Dao<Remind> remindDao) {
		this.remindDao = remindDao;
	}

	@Override
	public List<Remind> getRemindList(Remind remind, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from Remind where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(remind.getName())) {
			where.append(" and name like ?");
			params.add("%%" + remind.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(remind.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + remind.getContent() + "%%");
		}
		// 提醒事件发生日期
		if (!StringUtils.isBlank(remind.getHappenDate())) {
			where.append(" and happenDate like ?");
			params.add("%%" + remind.getHappenDate() + "%%");
		}
		// 提醒日期
		if (!StringUtils.isBlank(remind.getRemindDate())) {
			where.append(" and remindDate like ?");
			params.add("%%" + remind.getRemindDate() + "%%");
		}

		where.append(" order by createDate desc");
		List<Remind> list = remindDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Remind remind) {
		StringBuffer where = new StringBuffer();
		where.append("from Remind where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(remind.getName())) {
			where.append(" and name like ?");
			params.add("%%" + remind.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(remind.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + remind.getContent() + "%%");
		}
		// 提醒事件发生日期
		if (!StringUtils.isBlank(remind.getHappenDate())) {
			where.append(" and happenDate like ?");
			params.add("%%" + remind.getHappenDate() + "%%");
		}
		// 提醒日期
		if (!StringUtils.isBlank(remind.getRemindDate())) {
			where.append(" and remindDate like ?");
			params.add("%%" + remind.getRemindDate() + "%%");
		}
		
		return remindDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveRemind(Remind remind) {
		return remindDao.insert(remind);
	}

	@Override
	public boolean saveRemindList(List<Remind> remindList) {
		return remindDao.insert(remindList);
	}

	@Override
	public Remind getRemindById(Long timeKey) {
		return remindDao.getByKey(timeKey);
	}

	@Override
	public boolean updateRemind(Remind remind) {
		return remindDao.update(remind);
	}

	@Override
	public boolean delRemind(Long timeKey) {
		return remindDao.delete(timeKey);
	}

	@Override
	public List<Remind> getAllRemind() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return remindDao.query(where.toString(), null);
	}

	@Override
	public List<Remind> getRemindList(Remind remind) {
		StringBuffer where = new StringBuffer();
		where.append("from Remind where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(remind.getName())) {
			where.append(" and name like ?");
			params.add("%%" + remind.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(remind.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + remind.getContent() + "%%");
		}
		// 提醒事件发生日期
		if (!StringUtils.isBlank(remind.getHappenDate())) {
			where.append(" and happenDate like ?");
			params.add("%%" + remind.getHappenDate() + "%%");
		}
		// 提醒日期
		if (!StringUtils.isBlank(remind.getRemindDate())) {
			where.append(" and remindDate like ?");
			params.add("%%" + remind.getRemindDate() + "%%");
		}

		where.append(" order by createDate desc");
		List<Remind> list = remindDao.query(where.toString(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

}
